<?
// print_r($_REQUEST);
// exit;
include('/var/www/html/new/d/common/autoload.php');
if(!$_SESSION['sid']){
    echo '<script>alert("请先登录");</script>';
    exit;
}

$retain = array(
    'ideamov.12301.cc'=>'ideamov',
    'paypiao.12301.cc'=>'paypiao',
    'syyry.12301.cc'=>'syyry',
    'dtdc.12301.cc'=>'dtdc',
    'freeu.12301.cc'=>'freeu',
    'trfenxiao.12301.cc'=>'trfenxiao',
    'smf.12301.cc'=>'smf',
    'hubei.12301.cc'=>'hubei',
    'jqzj.12301.cc'=>'jqzj',
    'yyxpiao.12301.cc'=>'yyxpiao',
    'huangshan.12301.cc'=>'huangshan',
    'hjzl.12301.cc'=>'hjzl',
    'douqu.12301.cc'=>'douqu',
    'okpiao.12301.cc'=>'okpiao',
    '8youu.12301.cc'=>'8youu',
    'iyouke.12301.cc'=>'iyouke',
);
$conf = array(
    'fid'=>$_SESSION['sid'],
	'M_account_domain'=>$_SESSION['saccount'],
    'M_domain'=>$_REQUEST['SLD'],
    'M_name'=>$_REQUEST['web_name'],
    'M_qq'=>$_REQUEST['server_qq'],
    'M_tel'=>$_REQUEST['server_tel'],
    'M_logo1'=>$_REQUEST['web_logo'],
    'M_logo2'=>$_REQUEST['web_logo'],
    'M_banner'=>$_REQUEST['web_banner'],
    'M_banner_url'=>$_REQUEST['banner_url'],
    'M_host'=>$_REQUEST['alone_domain'],
    'M_addr'=>$_REQUEST['add_domain'],
);
//print_r($_POST);exit;
if(strlen($conf['M_domain'])>11 || strlen($conf['M_domain'])<5) {
    exit('<script>alert("二级域名名称必须大于5位并且小于12位");</script>');
}
$len = mb_strlen($conf['M_name'],'utf-8');
if($len>11 || $len<3) {
    exit('<script>alert("网站名称必须大于2位并且小于12位");</script>');
}
if(in_array($conf['M_domain'],$retain)){
    exit('<script>alert("很抱歉，您申请的域名为保留域名，请填写别的域名");</script>');
}
// exit;
if(!preg_match("/^(([a-z]+[0-9]+)|([0-9]+[a-z]+)|([0-9]+)|[a-zA-Z]+)[a-z0-9]*$/i",$conf['M_domain'])){
    exit('<script>alert("二级域名只能由字母和数字组成");</script>');
}
if($conf['M_banner_url'] && !isWeb($conf['M_banner_url'])){
    exit('<script>alert("链接地址不正确");</script>');
}
//if($conf['M_host'] && !isWeb($conf['M_host'])){
//    exit('<script>alert("独立域名不正确");</script>');
//}

// 域名是否存在
$sql="select id,fid from `pft_member_domain_info` where `M_domain`='{$conf['M_domain']}' limit 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc();
$id = $GLOBALS['le']->f('id');
if($id && ($GLOBALS['le']->f('fid')!=$_SESSION['sid'])){
    echo '<script>alert("域名已存在");</script>';
    exit;
}

// 不能是平台账号
$sql="select id from `pft_member` where account='{$conf['M_domain']}' limit 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc();
$mid = $GLOBALS['le']->f('id');
if($mid && $mid!=$_SESSION['sid']){
    echo '<script>alert("域名已存在");</script>';
    exit;
}

// 该账号是否配置
$sql="SELECT id FROM `pft_member_domain_info` WHERE `fid`='{$_SESSION['sid']}' LIMIT 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc();
$id = $GLOBALS['le']->f('id');

if(!$id && empty($conf['M_domain']) ) {
    exit('<script>alert("二级域名不能为空");</script>');
}

$ins="INSERT INTO `pft_member_domain_info` SET ";
$upd="UPDATE `pft_member_domain_info` SET ";
foreach($conf as $key=>$val){
    $upd.="`$key`='$val',";
    $ins.="`$key`='$val',";
}
$upd = rtrim($upd,',')." WHERE fid={$_SESSION['sid']} LIMIT 1";
$ins.='`createtime`=now()';

$result = ($_REQUEST['exid'] || $id) ? $GLOBALS['le']->query($upd):$GLOBALS['le']->query($ins);

// echo $upd;exit;
if($result){
    $redis = new Redis();
    $redis->connect('10.169.9.198', 6379);
    $redis->auth('myPft!12301!&');
    $redis_key = "shop_{$conf['M_account_domain']}";
    $redis->delete($redis_key);
    $redis->close();
    echo '<script>alert("配置成功");</script>';
    echo '<script>window.parent.location.reload();</script>';
    exit;
}else{
    echo '<script>alert("配置失败");</script>';
}

function isWeb($data){
    $regex = '@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@';
    return preg_match($regex, $data);
}
?>